"""
读取和保存yaml文件
"""
import yaml

from commons.models import CaseInfo
import logging

logger = logging.getLogger(__name__)

class YamlFile(dict):
    def __init__(self, path):
        super().__init__()  # 让对象按照原来的方式完成实例化--继承父类原有方法
        # 完成自定义代码
        self._path = path  # yaml路径
        self.load()  # 实例化YamlFile()对象时，自动加载yaml内容

    def load(self):
        with open(self._path, 'r', encoding='utf-8') as f:
            data = yaml.safe_load(f)
        if data:
            # data是个字典
            # self是个字典（继承dict=空的字典）
            self.update(data)  # 将2个字典合并

    def save(self):
        with open(self._path, 'w', encoding='utf-8') as f:
            # dict(self)： self是YamlFile，转成字典
            yaml.safe_dump(dict(self), f, allow_unicode=True)


if __name__ == '__main__':
    path = '../testcases/test_2_login.yaml'
    data = YamlFile(path)
    print(data)
    case_info = CaseInfo(**data)
